package demo1;

public class MyLinkedList {
    static class LinkedNode {
        public int val;
        public LinkedNode prev;
        public LinkedNode next;

        public LinkedNode(int val) {
            this.val = val;
        }
    }

    public LinkedNode head;
    public LinkedNode last;

    //头插法
    public void addHead(int data) {
        LinkedNode node = new LinkedNode(data);
        if (head == null) {
            head = last = node;
        } else {
            head.prev = node;
            node.next = head;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        LinkedNode node = new LinkedNode(data);
        if (head == null) {
            head = last = node;
        }
        node.prev = last;
        last.next = node;
        last = node;
    }

    //任意位置插入
    public void addindex(int index, int data) {
        int len = size();
        if (index < 0 || index > len) {
            return;
        }
        if (index == 0) {
            addHead(data);
            return;
        }
        if (index == len) {
            addLast(data);
            return;
        }
        LinkedNode node=new LinkedNode(data);
        LinkedNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    //查找是否包含关键字Key是否在双链表中
    public boolean contains(int key) {
        if (head == null) {
            return false;
        }
        LinkedNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字Key的节点
    public void remove(int key) {
        if (head == null) {
            return;
        } else if (head.val == key) {
            head = head.next;
            return;
        }
        LinkedNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                cur.prev.next = cur.next;
                if (cur.next != null) {
                    cur.next.prev = cur.prev;
                } else {
                    last = last.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有key的节点
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        while (head.val == key) {
            head = head.next;
        }
        LinkedNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                cur.prev.next = cur.next;
                if (cur.next != null) {
                    cur.next.prev = cur.prev;
                } else {
                    last = last.prev;
                }
            }
            cur = cur.next;
        }
    }

    //得到双链表的长度
    public int size() {
        int len = 0;
        LinkedNode cur = head;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    //清空双链表
    public void clear() {
        LinkedNode cur = head;
        while (cur != null) {
            LinkedNode curN = cur.next;
            cur.prev=null;
            cur.next = null;
            cur = curN;
        }
        head=last=null;
    }

    //打印单链表
    public void display() {
        LinkedNode cur = head;
        while (cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }
    }
}
